<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台默认控制器
class ContentController extends Controller{
	
    //展示首页
    public function index(){
    	//获得顶级分类,
    	$cateModel =new \Common\Model\Cate;
		$topCate = $cateModel->orderBy('cid')->get();
		View::with('topCate',$topCate);
//		 p($topCate);
		//获得当前位置
        //获得商品图册
        $gid=Q('get.gid',0,'intval');
		//实例化商品详情表
        $goodsModel=new \Common\Model\Detail;
		//获的商品详情表的数据
		$DetailData=$goodsModel->where("goods_gid='{$gid}'")->find();
		//将pics图片地址拆分
		if(!empty($DetailData['dpic'])){
			//把字符串拆分成数组
			$dpics = explode('|', $DetailData['dpic']);
			View::with('dpics',$dpics);
		}
//		p($DetailData);
		View::with('DetailData',$DetailData);
		//获得商品表数据
		$goodsModel=new \Common\Model\Goods;
		$goodsData=$goodsModel->where("gid={$gid}")->find();
//		p($goodsData);
		View::With('goodsData',$goodsData);
		//实例化商品属性表
		$goodsAttr=new \Common\Model\Goodsattr;
		//关联商品属性表和类型属性表,获得规格的数据
		$goodsattrData=$goodsAttr->join('typeattr','typeattr_taid','=','taid')->where("goods_gid={$gid} && class=1")->get();
		//获得颜色尺码
		$tepy=array();
		foreach ($goodsattrData as $k => $v) {
			$tepy[$v['taid']][]=$v;
		}
//		p($tepy);die;
		View::with('tepy',$tepy);
		//获得属性的数据
		$goodsattrData=$goodsAttr->join('typeattr','typeattr_taid','=','taid')->where("goods_gid={$gid} && class=0")->get();
		View::with('goodsattrData',$goodsattrData);
       	View::make();
    }
	//筛选商品数量
	public function ajaxData(){
	    //接受所传数据
	    if(IS_AJAX){
	    	//商品属性
	    	P($_POST);
	    	//所有的gid
	    	$gaid=Q('post.gaid');
//			p($gaid);
			//单个的gaid
			$gaidx=Q('post.gaid');
//			p($gaidx);
//			商品gid
			$gid=Q('post.gid');
//			p($gid);die;
			//去掉后面的逗号
			$newGaid=substr($gaid, 0,strlen($gaid)-1);
//			p($newGaid);die;
			$newGaid=explode(',', $newGaid);
//			p($newGaid);
			//除去选择gaid的组合   循环除了gaid之外的属性
			$arrgaid=array();
			foreach ($newGaid as $k => $v) {
				if($v!=$gaid){
					$arrgaid[]=$v;
				}
			}
//			p($arrgaid);
			//通过ajax传过来的数据查询商品的库存
			$aamodel=new \Common\Model\Goodslist;
			//给一个空数组
			$temp=array();
			//获得货品列表的所有数据
			$data=$aamodel->where("goods_gid={$gid}")->get();
			//循环数据
			p($data);
			foreach ($data as $k => $v) {
				$flag=0;
				//拆分转化成数组
				$data[$k]['combine']=explode('|', $v['combine']);
				foreach ($data[$k]['combine'] as $ke => $va) {
					if(!in_array($va, $newGaid)){
						$flag=1;
					}
				}
				if($flag==0){
					$temp=$v;
				}
				
				
			}
			p($temp);die;
			if(!empty($temp)){
				$temp['tprice'];
			}else{
				
			}
			//获取库存的所有id
			$comid=array();
			foreach ($data as $k => $v) {
				$data[$k]['combine']=explode('|', $v['combine']);
			}
			
//			p($data);
//			$combine=implode('|', $data['combine']);
//			$data=$aamodel->where("goods_gid={$gid}")->find();
//			p($data);
			//转化成数组
			
			//如果货品没货
			if(!$data){
				echo json_encode(0);
			}else{
				//根据gid查询商品属性表
				$bbmodel=new \Common\Model\Goodsattr;
				$attrData=$bbmodel->where("goods_gid={$gid} ")->get();
//				p($attrData);
				//给空数组
				$price=array();
				//将原价和附加价格加一起
				$oldprice=Q('post.price');

//				$price[]=$oldprice;
//				p($price);die;
				//旧价格+新价格
				foreach ($attrData as $k => $v) {
					$price[]=$oldprice+$v['added'];
				}
//				p($price);
//				P($data);die;
				//返回的数据  定义一个空数组
				$dataArr=array();
				foreach ($attrData as $k => $v) {
					if(in_array($v['gaid'],$data['combine'])){
						$dataArr['number']=$data['number'];
						$dataArr['inventory']=$data['inventory'];
						$dataArr['newprice']=array_sun($price);
					}
				}
				echo json_encode($dataArr);
				exit;
			}
	    }
	}
	//退出
	public function out(){
		session_unset();
		session_destroy();
		go(U('Content/index'));
	}
	
}
